<template>
  <el-container class="home-container">
    <el-header>
      <div class="title">
        <img src="../assets/icon/logo.jpg" alt />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="loginOut">loginOut</el-button>
    </el-header>
    <el-container>
      <el-aside :width="siderWidth">
        <div class="toggle-collapse" @click="toggleCollapse">|||</div>
        <el-menu
          :default-active="activeItem"
          class="el-menu-vertical-demo"
          background-color="#545c64"
          text-color="#fff"
          active-text-color="#ffd04b"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
        >
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i class="el-icon" :class="iconFontObj[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <template v-if="item.children.length">
              <el-menu-item :index="'/'+ current.path" v-for="current in item.children" :key="current.id" @click="saveNavActive(current)">
                <i class="el-icon iconfont icon-tijikongjian"></i>
                <span>{{current.authName}}</span>
              </el-menu-item>
            </template>
          </el-submenu>

        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  data() {
    return {
      menuList: [],
      iconFontObj: {
        '125': 'iconfont icon-users',
        '103': 'iconfont icon-lock_fill',
        '101': 'iconfont icon-shangpin',
        '102': 'iconfont icon-danju',
        '145': 'iconfont icon-baobiao'
      },
      activeItem: '',
      isCollapse: false
    }
  },
  created() {
    this.getMenuList()
    this.activeItem = window.sessionStorage.getItem('currentPath') || '0'
  },
  computed: {
    siderWidth() {
      return this.isCollapse ? '64px' : '200px'
    }
  },
  methods: {
    async getMenuList() {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.msg)
      this.menuList = res.data
      console.log(res)
    },
    loginOut() {
      console.log("tuchu")
    },
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    saveNavActive(active) {
      this.activeItem = '/' + active.path
      console.log(this.activeItem)
      window.sessionStorage.setItem('currentPath', '/' + active.path)
    }
  }
}
</script>

<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #273755;
  display: flex;
  justify-content: space-between;
  padding: 8px;
  .title {
    display: flex;
    align-items: center;
    img {
      width: 50px;
      height: 50px;
      border-radius: 50%;
    }
    span {
      color: #fff;
      margin-left: 10px;
    }
  }
}
.el-icon {
  margin-right: 5px;
}
.el-aside {
  background-color: #545c64;
  .el-menu {
    border:none;
  }
  .toggle-collapse {
    text-align: center;
    font-size: 10px;
    line-height: 24px;
    color:#fff;
    letter-spacing: .2em;
    background-color: #3c3c3c;
    cursor: pointer;
  }
}
.el-main {
  background-color: #f1f1f1;
}
</style>
